//
//  CTTestViewController.swift
//  elarc01
//
//  Created by elarc on 16/6/24.
//  Copyright © 2016年 elarc. All rights reserved.
//

import UIKit

class CTTestViewController: UIViewController,UIImagePickerControllerDelegate,UINavigationControllerDelegate,UITextViewDelegate {

    var lable:UILabel?
    var textView:UITextView?
    
    override func viewDidLoad() {
        super.viewDidLoad()
        view.backgroundColor = UIColor.whiteColor()
        lable = UILabel()
        lable?.frame = self.view.frame
        view.addSubview(lable!)
        
        textView = UITextView()
        textView!.frame = CGRectMake(0, ScreenHeight/2, ScreenWidth, ScreenHeight/2)
        textView!.delegate = self
    
        self.navigationItem.rightBarButtonItem = UIBarButtonItem(title:"添加",style: .Done, target: self, action: #selector(CTTestViewController.clickShareBtn))
        
        
//        let str = "这是一段用来测试的字符串 this is a string for test"
//        let dic = [NSFontAttributeName:UIFont.boldSystemFontOfSize(20),
//            NSForegroundColorAttributeName:UIColor.redColor()]
////        let attrStr = NSAttributedString(string: str, attributes: dic)
////        label.attributedText = attrStr
////        label.textColor = UIColor.yellowColor()
//
//        let mutableAttrStr = NSMutableAttributedString(string: str)
//        mutableAttrStr.addAttributes(dic, range: NSMakeRange(0, 2))
//        mutableAttrStr.addAttributes([NSFontAttributeName:UIFont.systemFontOfSize(13),NSUnderlineStyleAttributeName: 1 ], range: NSMakeRange(2,8))
//        label.attributedText = mutableAttrStr


//        let ctView = CTView()
//        ctView.frame = CGRectMake(10, 150, self.view.bounds.width - 20, 200)
//        ctView.backgroundColor = UIColor.whiteColor()
//        self.view.addSubview(ctView)
        
        let cptView = CTPicTxtView()
        cptView.frame = CGRectMake(10, 100, self.view.bounds.width - 20, 600)
        cptView.backgroundColor = UIColor.whiteColor()
        self.view.addSubview(cptView)
        
        view.addSubview(textView!)
//        let font = UIFont.systemFontOfSize(14)
//        print(font.descender)       //-3.376953125
//        print(font.ascender)        //13.330078125
//        print(font.lineHeight)      //16.70703125
//        print(font.capHeight)       //9.8642578125
//        print(font.xHeight)         //7.369140625
//        print(font.leading)         //0.0

//        let ctextView = CTextView()
//        ctextView.frame = CGRectMake(10, 100, self.view.bounds.width - 20, 200)
//        ctextView.backgroundColor = UIColor.whiteColor()
//        self.view.addSubview(ctextView)
//
//
//        let ctURLView = CTURLView()
//        ctURLView.frame = CGRectMake(10, 100, self.view.bounds.width - 20, 300)
//        ctURLView.backgroundColor = UIColor.grayColor()
//        let mutableAttrStr = NSMutableAttributedString(string: ctURLView.str)
//        let size = ctURLView.sizeForText(mutableAttrStr)
//        ctURLView.frame.size = size
//        self.view.addSubview(ctURLView)
        

        // Do any additional setup after loading the view.
    }
    
    func clickShareBtn() -> Void {
        let alertController = UIAlertController(title: nil, message: "添加", preferredStyle: .Alert)
        let carmeraAction = UIAlertAction(title: "手机拍摄", style: .Default) { (action) in
            self.selectImage(UIImagePickerControllerSourceType.Camera, prompt: "相机")
        }
        let photoAction = UIAlertAction(title: "从系统相册选取", style: .Default) { (action) in
            self.selectImage(UIImagePickerControllerSourceType.PhotoLibrary, prompt: "相机")
        }
        alertController.addAction(carmeraAction)
        alertController.addAction(photoAction)
        self.presentViewController(alertController, animated: true, completion: nil)
    }
    
    func selectImage(type:UIImagePickerControllerSourceType,prompt:String) -> Void {
        
        if UIImagePickerController.isSourceTypeAvailable(type) == true {
            
            let pickerVc = UIImagePickerController()
            pickerVc.delegate = self
            pickerVc.allowsEditing = true
            pickerVc.sourceType = type
            
            let delegate = UIApplication.sharedApplication().delegate as! AppDelegate
            delegate.window?.rootViewController?.presentViewController(pickerVc, animated: true, completion: { 
                
            })
            
        }
    }
    
    func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) {
        
        var image = info[UIImagePickerControllerEditedImage]
        if (image == nil) {
            image = info[UIImagePickerControllerOriginalImage];
        }
//        self.picture = image;
        
        let attri = NSMutableAttributedString(string: (textView?.text)!)
        attri.addAttribute(NSFontAttributeName, value: UIFont(name: "HelveticaNeue-Bold", size: 16)!,
                                     range: NSMakeRange(0,(textView?.text.characters.count)!))
        
        let attach = NSTextAttachment()
        attach.image = image as? UIImage
        
        //大小自己调整
        attach.bounds = CGRectMake(0, -10, ScreenWidth - 10,200);
        
        let string = NSAttributedString(attachment: attach)
        attri.appendAttributedString(string)

        textView!.attributedText = attri
        
        picker.dismissViewControllerAnimated(true) { 
            
        }
    }
    
    func imagePickerControllerDidCancel(picker: UIImagePickerController) {
        picker.dismissViewControllerAnimated(true) { 
            
        }
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */

}
